package cn.client.ui.view.main.mindfulness

import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import cn.client.R
import cn.client.data.remoteData.bean.AddMindfulnessLogBody
import cn.client.data.userStateManager
import cn.client.ui.components.BaseTopBar
import cn.client.ui.components.SubmitButton
import kotlinx.coroutines.flow.cancellable
import kotlinx.coroutines.launch


// 新增打卡页面
@Composable
fun MindfulnessClockInScreen(
    mid: Int,
    navController: NavController,
    viewModel: MindfulnessViewModel = hiltViewModel()
){
    val userIdState = userStateManager.userId.collectAsState(initial = -1)
    val userId = userIdState.value
    val coroutineScope = rememberCoroutineScope()
    val context = LocalContext.current

    // 文本内容
    var content: String by remember { mutableStateOf("") }

    LaunchedEffect(Unit) {
        viewModel.pageEvents.collect {
            when(it) {
                is MindfulnessViewModel.PageEvent.Error -> {
                    Toast.makeText(context, "提示：${it.error}", Toast.LENGTH_LONG).show()
                }
                is MindfulnessViewModel.PageEvent.Success -> {
                    // 返回上一级
                    navController.navigateUp()
                }
                else -> {}
            }
        }
    }

    Scaffold(
        topBar = {
            BaseTopBar(
                title = "正念打卡",
                onClickArrowBack = {
                    coroutineScope.launch {
                        // 返回上一级
                        navController.navigateUp()
                    }
                }
            )
        }
    ) { it ->
        Column(
            modifier = Modifier
                .padding(it)
                .fillMaxSize()
        ){
            HomeSection(title = "请输入打卡心得"){
                /* 内容输入框 */
                Row(
                    horizontalArrangement = Arrangement.Center,
                    modifier = Modifier
                        .fillMaxWidth()
                        .wrapContentHeight()
                        .padding(5.dp)
                ) {
                    TextField(
                        value = content,
                        onValueChange = { str -> content = str },
                        placeholder = { Text(stringResource(R.string.mindfulness_input_info)) },
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentHeight()
                            .padding(5.dp)
                    )
                }
            }
            // 提交
            SubmitButton(
                onClickSubmitButton = {
                    coroutineScope.launch {
                        /* 发送新增打卡意图 */
                        val add = AddMindfulnessLogBody(
                            mid = mid,
                            uid = userId,
                            experience = content
                        )
                        viewModel.dispatch(MindfulnessViewModel.PageIntent.AddLog(add))
                    }
                }
            )
        }
    }
}